﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ivory;

namespace Ivory.GUI
{
    public class Image : GUIComponent
    {
        //Fields 
        private string ImageLocation;

        private Vector2 _Location = Vector2.Zero; 
		public Vector2 Location
		{
			get { return _Location; }
			set { _Location = value; }
		}

        private Texture2D _Img; 
		public Texture2D Img
		{
            get { return _Img; }
            set { _Img = value; }
		} 
 
		private Color _ImageShade = Color.White; 
		public Color ImageShade
		{
			get { return _ImageShade; }
			set { _ImageShade = value; }
		} 	
 
		private bool _IgnoreResize; 
		public bool IgnoreResize
		{
			get { return _IgnoreResize; }
			set { _IgnoreResize = value; }
		} 

		private Vector2 _ImageOrigin = Vector2.Zero; 
		public Vector2 ImageOrigin
		{
			get { return _ImageOrigin; }
			set { _ImageOrigin = value; }
		} 
		
        //Construct
        public Image(Game game, string TextureLocation, Vector2 Position)
            : base(game, new Rectangle((int)Position.X, (int)Position.Y, 0, 0))
        {
            this.ImageLocation = TextureLocation;
            this.Location = Position;
        }
        public Image(Game game, string TextureLocation, Rectangle Position)
            : base(game, Position)
        {
            this.ImageLocation = TextureLocation;
            this.Location = new Vector2(Position.X, Position.Y);
        }

        public Image(Game game, Texture2D Texture, Vector2 Position)
            : base(game, new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height))
        {
            this.Img = Texture;
            this.Location = Position;
            this.ImageOrigin = new Vector2(Img.Width, Img.Height) / 2;
        }
        public Image(Game game, Texture2D Texture, Rectangle Position)
            : base(game, Position)
        {
            this.Img = Texture;
            this.Location = new Vector2(Position.X, Position.Y);
            this.ImageOrigin = new Vector2(Img.Width, Img.Height) / 2;
        }
        
        //Methods
        protected override void ResizeComponent()
        {
            if (!IgnoreResize)
            {
                this.ClickRectangle = new Rectangle((int)Location.X, (int)Location.Y, Img.Width, Img.Height);
                ImageOrigin = new Vector2(Img.Width, Img.Height) / 2;
            }
        }

        public override void DrawComponent(SpriteBatch batch, GameTime time)
        {
            batch.Draw(Img, ClickRectangle, ImageShade);
        }

        public override void LoadComponent()
        {
            if (string.IsNullOrEmpty(ImageLocation))
            {
                if (Img == null)
                    throw new IvoryFrameworkError(Errors.AssetLoadingException);
            }

            Img = Game.Content.Load<Texture2D>(ImageLocation);
            ResizeComponent();
        }
    }
}
